package com.atguigu.project;

import com.atguigu.been.AdsClickLog;
import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author wky
 * @create 2021-07-16-11:50
 */
public class Project_Ads_Click {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment senv = StreamExecutionEnvironment.getExecutionEnvironment();
        senv.setParallelism(1);
        senv.setRuntimeMode(RuntimeExecutionMode.BATCH);
        DataStreamSource<String> streamSource = senv.readTextFile("src/input/AdClickLog.csv");
        SingleOutputStreamOperator<AdsClickLog> map = streamSource.map(new MapFunction<String, AdsClickLog>() {
            @Override
            public AdsClickLog map(String s) throws Exception {
                String[] datas = s.split(",");
                return new AdsClickLog(
                        Long.valueOf(datas[0]),
                        Long.valueOf(datas[1]),
                        datas[2],
                        datas[3],
                        Long.valueOf(datas[4]));
            }
        });

        SingleOutputStreamOperator<Tuple2<String, Long>> map1 = map.map(new MapFunction<AdsClickLog, Tuple2<String, Long>>() {
            @Override
            public Tuple2<String, Long> map(AdsClickLog adsClickLog) throws Exception {
                return Tuple2.of(adsClickLog.getProvince() + "-" + adsClickLog.getAdId(), 1L);
            }
        });
        KeyedStream<Tuple2<String, Long>, Tuple> tuple2TupleKeyedStream = map1.keyBy(0);
        tuple2TupleKeyedStream.sum(1).print("省份-广告");

        senv.execute();
    }
}
